<?php 
/**
 * 订单控制器
 */
class OrderController extends Controller{
    /**
     * 
     */
    private $model;
    
    public function index(){
    	if(isset($_SESSION['uid'])){
    		$cart = new Cart();
			$uid = $_SESSION['uid'];
			$ADress_data = K('Address')->where("user_uid={$uid}")->all();
			$this->assign('ADress_data',$ADress_data);
			
			//默认收货地址信息
			$First_data = K('Address')->where("user_uid={$uid}")->find();
			$this->assign('First_data',$First_data);
			
			//获得所有商品信息
			$Gdata = $cart->getAllData();
			
			$this->assign('Gdata',$Gdata);
	        $this->display(); 
    	}else{
    		$this->error('您还未登陆，请登录……',U('Login/index'));
    	}
    	
    }
	
	/**
	 * 添加地址
	 */
	public function address(){
		if(IS_POST){
			$_POST['province'] = $_POST['province'].'&nbsp;'.$_POST['city'].'&nbsp;'.$_POST['county'];
			$this->model = K('Address');
			if(!$this->model->addData()){
				$this->error($this->model->error);
			}
			$this->success('添加地址成功');
		}
    }
	
	/**
	 * 添加订单
	 */
	public function addOrder(){
		
	    if(IS_POST){
	    	//订单号
	    	$yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
			$orderSn = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
			//地址信息
	    	$id = Q('post.info',0,'intval');
			$info = K('Address')->where("adid={$id}")->find();
			$_POST['number'] = $orderSn;
			$_POST['consignee'] = $info['Uname'];
			$_POST['mobile'] = $info['phone'];
			$_POST['address'] = $info['province'].$info['address'];
			$_POST['user_uid'] = $_SESSION['uid'];
			$this->model = K('Order');
			if(!$oid = $this->model->addData()){
				$this->error($this->model->error);
			}
			//清空session
			unset($_SESSION['cart']);
			$this->success('订单已生成，请尽快支付……', U('pay',array('oid'=>$oid)));
		}
	}
	
	/**
	 * 订单支付页
	 */
	public function pay(){
		$oid = Q('get.oid',0,'intval');
		$info = K('Order')->where("oid={$oid}")->find();
		$shopInfo = M()->join('__order_list__ ol JOIN __goods__ g ON ol.goods_gid=g.gid')->where("order_oid={$oid}")->all();
		$this->assign('info',$info);
		$this->assign('shopInfo',$shopInfo);
	    $this->display();
	}
	
	/**
	 * 订单支付成功
	 */
	public function paySuccess(){
		$oid = Q('get.oid',0,'intval');
		K('Order')->where("oid={$oid}")->update(array('status'=>'已付款'));
		
		$this->success('支付成功，请耐心等待发货……', U('OrdCen/index'));
	    $this->display();
	}
	
	/**
	 * 异步获得订单信息(收货信息区域)
	 */
	public function addsInfo(){
	    if(IS_AJAX){
	    	$adid = Q('post.adid',0,'intval');
	    	$addsInfo = K('Address')->where("adid={$adid}")->find();
			echo json_encode($addsInfo);
			exit;
	    }
	}
	
 }
 
 
 
 
 
 
 
 
 
 
 




 ?>